package com.crm.service.impl.finishOrderProductManager;

import com.crm.dao.mapper.customMapper.returnMoneyManagerCustomMapper.FinishOrderProductCustomMapper;
import com.crm.dao.mapper.finishOrderProduct.FinishOrderProductMapper;
import com.crm.dao.mapper.returnMoney.ReturnMoneyFinishOrderProductMapper;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.finishOrderProduct.FinishOrderProductExample;
import com.crm.model.entity.orderManager.CustomerOrderActivityRelation;
import com.crm.model.entity.orderManager.CustomerOrderActivityRelationExample;
import com.crm.model.qo.finishOrderProductManager.AnewSendFinishProductQO;
import com.crm.model.qo.finishOrderProductManager.FinishOrderProductQO;
import com.crm.model.vo.finishOrderProduct.*;
import com.crm.model.vo.orderManager.CustomerProductOrderVO;
import com.crm.service.finishOrderProductManager.FinishOrderProductDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/7/30 14:42
 */
@Service
public class FinishOrderProductDbServiceImpl implements FinishOrderProductDbService {

    @Autowired
    private ReturnMoneyFinishOrderProductMapper returnMoneyFinishOrderProductMapper;

    @Autowired
    private FinishOrderProductMapper finishOrderProductMapper;
    @Autowired
    private FinishOrderProductDbService finishOrderProductDbService;
    @Autowired
    private FinishOrderProductCustomMapper finishOrderProductCustomMapper;

    /**
     * 查询完成订单的产品
     * @param order
     * @param qo
     * @return
     */
    @Override
    public List<FinishOrderProductVO> selectCustomerProductList(String order, FinishOrderProductQO qo,Integer dateUnit) {
        return returnMoneyFinishOrderProductMapper.selectCustomerProductList(order,qo,dateUnit);
    }

    /**
     * 修改完成订单的产品信息
     * @param finishOrderProduct
     */
    @Override
    public boolean updateFinishOrderProduct(FinishOrderProduct finishOrderProduct) {
        return finishOrderProductMapper.updateByPrimaryKeySelective(finishOrderProduct) > 0;
    }

    /**
     * 查询未发放提成的产品
     * @return
     */
    @Override
    public List<ExportFinishOrderProductVO> selectExportFinishOrderProduct(String companyId,Integer dateUnit) {
        return returnMoneyFinishOrderProductMapper.selectExportFinishOrderProduct(companyId,dateUnit);
    }

    /**
     * 根据产品号查询当前产品录没录提成
     * @param finishOrderProductNum
     */
    @Override
    public String selectOrderProductRoyaltyStatus(String finishOrderProductNum) {
        return returnMoneyFinishOrderProductMapper.selectOrderProductRoyaltyStatus(finishOrderProductNum);
    }

    /**
     * 根据产品号查询
     * @param finishOrderProductNum
     * @return
     */
    @Override
    public FinishOrderProductVO selectFinishOrderProductByNumber(String finishOrderProductNum) {
        return returnMoneyFinishOrderProductMapper.selectFinishOrderProductByNumber(finishOrderProductNum);
    }

    /**
     * 通过订单id和订单明细类型查询为产品类型为  1 的产品
     * @param productOrderId 订单id
     * @param type 订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:兑换类型
     * @return
     */
    @Override
    public List<FinishOrderProduct> selectCustomerProductListByOrderId(String productOrderId, String type) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andOrderDetailTypeEqualTo(type);
        criteria.andDelFlagEqualTo("0");
        //criteria.andProductStatusEqualTo("0");
        List<FinishOrderProduct> list = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        return list;
    }

    /**
     * 查询需要补发产品的信息
     * @param order
     * @param qo
     * @param dateUnit
     * @return
     */
    @Override
    public List<AnewSendFinishProductVO> selectAnewSendFinishProduct(String order, AnewSendFinishProductQO qo, Integer dateUnit) {
        return returnMoneyFinishOrderProductMapper.selectAnewSendFinishProduct(order,qo,dateUnit);
    }

    @Override
    public List<FinishOrderProductVO> selecttestFinishList() {
        return returnMoneyFinishOrderProductMapper.selectTestFinishList();
    }

    /**
     * 查询需要同步finish表合同编号订单编号客户名称的
     * @return
     */
    @Override
    public List<FinishOrderProductVO> selectOrderNumberIsNull() {
        return returnMoneyFinishOrderProductMapper.selectOrderNumberIsNull();
    }

    /**
     * 通过finishId查询finsh产品数据
     * @param finishOrderProductId 订单完成产品id
     * @return
     */
    @Override
    public FinishOrderProduct selectFinishOrderProductById(String finishOrderProductId) {
        return finishOrderProductMapper.selectByPrimaryKey(finishOrderProductId);
    }

    /**
     * 查询订单下的finish表数据（不含赠品）
     * @param orderId
     * @return
     */
    @Override
    public List<FinishOrderProductVO> selectOrderFinishProduct(String orderId,Integer type) {
        return returnMoneyFinishOrderProductMapper.selectOrderFinishProduct(orderId,type);
    }

    /**
     * 查询余额支付的订单下的产品（不含赠品）
     * @param customerId
     * @return
     */
    @Override
    public List<FinishOrderProductVO> selectBalanceOrderFinishProduct(String customerId) {
        return returnMoneyFinishOrderProductMapper.selectBalanceOrderFinishProduct(customerId);
    }

    @Override
    public List<FinishOrderProductVO> selectFinishNumberByOrderIdAndProduct(String orderId) {
        return returnMoneyFinishOrderProductMapper.selectFinishNumberByOrderIdAndProduct(orderId);
    }

    @Override
    public List<FinishOrderProduct> selectFinishByOrderIdAndProductNumber(String productOrderId, String productName) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andDelFlagEqualTo("0");
        //criteria.andProductStatusEqualTo("3");
        if (Objects.nonNull(productName)){
            criteria.andProductIdEqualTo(productName);
        }
        List<FinishOrderProduct> list = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        return list;
    }

    @Override
    public List<FinishOrderProductVO> selectFinish19(String orderId,String productId) {
        return returnMoneyFinishOrderProductMapper.selectFinish19(orderId,productId);
    }

    @Override
    public List<FinishOrderProductVO> selectFinish129() {
        return returnMoneyFinishOrderProductMapper.selectFinish129();
    }

    /**
     * 批量补充订单完成表客户信息(临时用补充正式库数据:customer_name为空的)
     * @param finishOrderProductList
     * @author yincuimei
     * @Date 2021-09-28
     * @return
     */
    @Override
    public Boolean batchUpdateFinishOrderProductByOrderId(List<FinishOrderProduct> finishOrderProductList) {
        return returnMoneyFinishOrderProductMapper.batchUpdateFinishOrderProductByOrderId(finishOrderProductList) > 0;
    }

    @Override
    public Boolean updateArriveTimeAndOtherSelectiveInfo(FinishOrderProduct finishOrderProduct) {
        return finishOrderProductCustomMapper.updateArriveTimeAndOtherSelectiveInfo(finishOrderProduct);
    }

    @Override
    public int updateArriveTimeAndOtherSelectiveInfoByProductOrderId(FinishOrderProduct fop) {
        return finishOrderProductCustomMapper.updateArriveTimeAndOtherSelectiveInfoByProductOrderId(fop);
    }

    @Override
    public List<FinishOrderProductVO> selectListByProductOrderId(String orderId, String productStatus) {
        return finishOrderProductCustomMapper.selectListByProductOrderId(orderId, productStatus);
    }

    /**
     * opp客户购买产品
     * @param list
     * @return
     */
    @Override
    public List<ExportOppFinishProductVO> exportOppCustomerFinishProduct(List customerList) {
        return finishOrderProductCustomMapper.exportOppCustomerFinishProduct(customerList);
    }
}
